package com.module.product.dao;

import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import base.dao.GenericHibernateDao;
import base.util.EntityUtil;

import com.module.product.vo.ProductTemporarySyn;
import com.module.product.vo.SynStatus;

/**
 * 
 * 同步商品dao类
 * 
 * @author zhangjy
 */
public class ProductTemporarySynDao  extends GenericHibernateDao<ProductTemporarySyn, Long >{
	private static final ProductTemporarySynDao instance = new ProductTemporarySynDao();

	private ProductTemporarySynDao() {
	}

	public synchronized static ProductTemporarySynDao getInstance() {
		return instance;
	}

	SynStatusDao synStatusDao =  SynStatusDao.getInstance();
	/**
	 * 根据企业ID获取需要同步的数据
	 * @param session
	 * @param orgid
	 * @return
	 */
	public List<ProductTemporarySyn> list(Session session, String orgid ) {
		
		String tableName = EntityUtil.getTableName(ProductTemporarySyn.class);
    	Long syscId = synStatusDao.getIdByorgIdAndTableName(session,orgid,tableName);
		
		List<ProductTemporarySyn> list = null;
		StringBuffer hql = new StringBuffer();
		hql.append("from ProductTemporarySyn o where o.issysc ='1' and o.iSyscId > ? and o.orgId ='"
				+ orgid + "' order by o.iSyscId ");
		Query query = session.createQuery(hql.toString());
		query.setParameter(0,syscId);
		list = query.list();
		 if(list != null && list.size()>0){
	        	//获取同步数据后，保存同步记录到synStatus
	        	SynStatus synStatus =  new SynStatus();
	        	synStatus.setiSyscId(list.get(list.size()-1).getiSyscId());
	        	synStatus.setOrgId(orgid);
	        	synStatus.setSyscTime(new Date());
	        	synStatus.setTableName(tableName);
	        	synStatusDao.save(session, synStatus);
	        	
	    }
		return list;
	}
	

}
